package com.mr.service;

import com.mr.client.BrandClient;
import com.mr.client.CategoryClient;
import com.mr.client.GoodsClient;
import com.mr.client.SpecClient;
import com.mr.service.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ItemService
 * @Description: TODO
 * @Author kun
 * @Date 2020/8/18
 * @Version V1.0
 **/
@Service
public class ItemService {

    @Autowired
    private BrandClient brandClient;
    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private GoodsClient  goodsClient;
    @Autowired
    private SpecClient specClient;

    /**
     * 获得模型数据
     * @param spuId
     * @return
     */
    public Map<String,Object> getInfoModel(Long spuId){
        //spu数据
        Spu spu= goodsClient.querySpuById(spuId);
        //spu 详情
        SpuDetail spuDetail= goodsClient.queryDetail(spuId);
        //skus
        List<Sku> skuList=goodsClient.querySkus(spuId);

        //分类
        List<CategoryEntity> categoryList=categoryClient.queryCateLisByIds(Arrays.asList(spu.getCid1(),spu.getCid2(),spu.getCid3()));

        //品牌
        BrandEntity brand=brandClient.queryBrandById(spu.getBrandId());
        //规格组
        List<SpecGroup> specGroupList=specClient.querySpecGroupList(spu.getCid3());
        //规格组填充规格详细 需要在specGroup中加入specParam集合，加忽略注解
        specGroupList.forEach(specGroup -> {
            specGroup.setSpecParamList( specClient.querySpecParam(specGroup.getId(),null,null,null));
        });
        //特有规格参数
        List<SpecParam> specParamList=specClient.querySpecParam(null,spu.getCid3(),null,false);
        Map<Long,String> specParamMap=new HashMap();
        specParamList.forEach(specParam -> {
            specParamMap.put(specParam.getId(),specParam.getName());
        });
        //组装所有数据，返回页面
        Map<String,Object> modelMap =new HashMap<>();
        modelMap.put("spu",spu);
        modelMap.put("spuDetail",spuDetail);
        modelMap.put("skuList",skuList);
        modelMap.put("categoryList",categoryList);
        modelMap.put("brand",brand);
        modelMap.put("specGroupList",specGroupList);
        modelMap.put("specParamMap",specParamMap);

        return modelMap;
    }
}